package com.example.administrator.sixangleview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.example.administrator.sixangleview.view.SixangleImageViews;
import com.example.administrator.sixangleview.view.SixangleViewGroup;

import java.lang.ref.WeakReference;

public class MainActivity extends Activity {
    private SixangleViewGroup sixangleViewGroup;
    private SixangleImageViews sixangleImageViews;
    private ViewBean viewBean;
    private boolean isBegin;
    private boolean isThreadStart;
    private SecurityHandle handle;
    private Button actBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        isBegin = false;
        isThreadStart = false;
        handle = new SecurityHandle(this);
        sixangleViewGroup = (SixangleViewGroup) findViewById(R.id.sixangleViewGroup);
        initViewGroup();
        actBtn = (Button) findViewById(R.id.action_btn);
        actBtn.setOnClickListener(new ActionBtnListener());
    }

    public void initViewGroup() {
        for (int i = 0; i < 7; i++) {
            viewBean = new ViewBean();
            viewBean.setCode(i);
            viewBean.setColor(i);
            viewBean.setTextSize(24);
            viewBean.setTexts(setName(i));
            sixangleImageViews = new SixangleImageViews(this, viewBean);
            sixangleViewGroup.addView(sixangleImageViews);
        }
    }

    public String setName(int key) {
        switch (key) {
            case 0:
                return "飞机票";
            case 1:
                return "火车票";
            case 2:
                return "酒店";
            case 3:
                return "用户";
            case 4:
                return "促销";
            case 5:
                return "提醒";
            case 6:
                return "设置";
        }
        return "";
    }

    private class ActionBtnListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (!isThreadStart) {
                isThreadStart = true;
                isBegin = true;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                if (!isBegin) {
                                    Thread.sleep(10);
                                    continue;
                                }
                                Thread.sleep(20);
                                handle.sendEmptyMessage(1);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            } else {
                isBegin = !isBegin;
            }

            if (isBegin)
                actBtn.setText(getResources().getText(R.string.stop_symbol));
            else
                actBtn.setText(getResources().getText(R.string.start_symbol));
        }
    }

    private static class SecurityHandle extends Handler {
        private WeakReference<MainActivity> weakReference;

        public SecurityHandle(MainActivity mainActivity) {
            weakReference = new WeakReference<>(mainActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            MainActivity outer = weakReference.get();
            if (outer != null) {
                if (msg.what == 1) {
                    outer.sixangleViewGroup.changeChildLayout(1);
                }
            }
        }
    }

    public class ViewBean {
        private int color;
        private float textSize;
        private int code; // 编号
        private String texts;

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public float getTextSize() {
            return textSize;
        }

        public void setTextSize(float textSize) {
            this.textSize = textSize;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getTexts() {
            return texts;
        }

        public void setTexts(String texts) {
            this.texts = texts;
        }
    }

}